package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.ApplyDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Apply;

public class ApplyDaoImpl extends BaseDaoImpl<Apply> implements ApplyDao{

	@Override
	public Collection<Apply> getAllApply() {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Apply.class);
		return criteria.list();
	}

	@Override
	public Collection<Apply> getApplyByIds(Collection<Integer> ids) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Apply.class);
		criteria.add(Restrictions.in("id", ids));
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<?> getReportApplySource(final Integer state, final String level,
			final Date datestart, final Date dateend, final Collection<Person> persons) {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Apply.class);
				if(state != null)
					criteria.add(Restrictions.eq("flowstatus", state));
				else
					criteria.add(Restrictions.isNull("flowstatus"));
				
				if(level != null)
					criteria.add(Restrictions.eq("level", level));
				if(datestart != null)
					criteria.add(Restrictions.ge("recordTime", datestart));
				if(dateend != null)
					criteria.add(Restrictions.le("recordTime", dateend));
				if(persons != null)
					criteria.add(Restrictions.in("recorder", persons));
				
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("recorder"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
	        	
				return criteria.list();
			}});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Apply> getValidCritiqueApply(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<Apply>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria =arg0.createCriteria(Apply.class);
				criteria.add(Restrictions.eq("flowstatus", Constrains.WORKFLOWENTITY_STATE_PROCESS));
				criteria.createAlias("copyToPersons", "c").add(Restrictions.eq("c.id", person.getId()));				
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Apply> getExistAuditInfoByPerson(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<Apply>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Apply.class);
				criteria.add(Restrictions.eq("recorder", person));
				criteria.add(Restrictions.or(Restrictions.ne("flowstatus", Constrains.WORKFLOWENTITY_STATE_INIT), Restrictions.ne("flowstatus", Constrains.WORKFLOWENTITY_STATE_NOT)));
				criteria.addOrder(Order.asc("flowstatus"));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

}
